﻿

using IoTClient;
using IotClientService.Extensions;
using IotClientService.Handles.lgs;
using IotClientService.Models.Lg;
using IotClientService.Models.Lg.Remote;
using IotContract.Dtos.Remotes.Request;
using IotContract.Dtos.Servers.Process;
using IotContract.Extensions;
using IotContract.Models;
using IotContract.Models.Clients.Hks;
using Mapster;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XT.Common.Enums;
using XT.Common.Extensions;
using XT.Common.Interfaces;
using XT.Common.Models.Server;
using XT.Common.Services;

namespace IotClientService.Services;
public class LgCallApiService : ApiConfigService, ILgApiService
{

    private string _apiHead;
    private IHttpClientFactory _httpClientFactory;
    private ILogService _logService;
    private ISignalRService _signalRService;
    private string Token;

    public LgCallApiService(IHttpClientFactory httpClientFactory, ISignalRService signalRService, ILogService logService) : base(httpClientFactory)
    {
        _signalRService = signalRService;
        _logService = logService;
    }



    #region 组盘机
    /// <summary>
    /// 当托盘进入后，触发接口进行验证
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitAssembleTrayEnter(LgTrayEnterRequest lgTrayEnterRequest)
    {
        var client = CreateHttpClient();
        //string url = $"{_apiHead}/api/assembleTray/commit-tray-enter"; 
        string url = $"{_apiHead}/fms/api/assembleTray/commit-assemble-tray-enter"; 
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayEnterRequest);
        JudgeResult(result.Success, "CommitAssembleTrayEnter", lgTrayEnterRequest, result);
        return result;
    }
    /// <summary>
    /// 提交组盘机挑选结果 ,结果为 托盘+4个电芯，每挑选4个触发一次
    /// </summary>
    /// <param name="cellResultRequest"></param>
    /// <returns></returns>

    public async Task<AdminCodeResult<LgNgResult>> CommitAssembleResult(LgCellResultRequest cellResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/assembleTray/commit-assemble-result"; 
        var result = await client.PostFmsData<LgNgResult>(url, cellResultRequest);
        JudgeResult(result.Success, "CommitAssembleResult", cellResultRequest, result);
        return result;
    }

    /// <summary>
    /// 组盘机作业开始、作业结束报告
    /// </summary>
    /// <param name="lgAssembleReportRequest"></param>
    /// <returns></returns>

    public async Task<AdminCodeResult<LgNgResult>> CommitAssembleReport(LgAssembleReportRequest lgAssembleReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/assembleTray/commit-assemble-report";
        var result = await client.PostFmsData<LgNgResult>(url, lgAssembleReportRequest);
        JudgeResult(result.Success, "CommitAssembleReport", lgAssembleReportRequest, result);
        return result;
    }
    /// <summary>
    /// 托盘出组盘上报
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitAssembleTrayOut(LgTrayOutRequest lgTrayOutRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/assembleTray/commit-tray-leave";
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayOutRequest);
        JudgeResult(result.Success, "CommitAssembleTrayLeave", lgTrayOutRequest, result);
        return result;
    }
    #endregion

    #region 拆盘机
    /// <summary>
    /// 拆盘机托盘进入，触发接口进行验证
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitDisAssembleTrayEnter(LgTrayEnterRequest lgTrayEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-dis-assemble-tray-enter";
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayEnterRequest);
        JudgeResult(result.Success, "CommitDisAssembleTrayEnter", lgTrayEnterRequest, result);
        return result;
    }
    /// <summary>
    /// 拆盘机托盘出设备上报
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitDisAssembleTrayOut(LgTrayOutRequest lgTrayOutRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-dis-assemble-tray-out";
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayOutRequest);
        JudgeResult(result.Success, "CommitDisAssembleTrayOut", lgTrayOutRequest, result);
        return result;
    }
    /// <summary>
    /// 拆盘机作业开始、作业结束报告
    /// </summary>
    /// <param name="lgTrayReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitDisAssembleReport(LgTrayReportRequest lgTrayReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-dis-assemble-report";
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayReportRequest);
        JudgeResult(result.Success, "CommitDisAssembleReport", lgTrayReportRequest, result);
        return result;
    }
    /// <summary>
    /// 获取托盘电芯位置
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgTrayPositionResult>> GetTrayPositionResult(LgTrayEnterRequest lgTrayEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/get-tray-position-result";
        var result = await client.PostFmsData<LgTrayPositionResult>(url, lgTrayEnterRequest);
        JudgeResult(result.Success, "GetTrayPositionResult", lgTrayEnterRequest, result);
        return result;
    }
    #endregion

    #region 清洗机
    /// <summary>
    /// 提交清洗机结果
    /// </summary>
    /// <param name="lgClearDataRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitClearResult(LgClearDataRequest lgClearDataRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-clear-result";
        var result = await client.PostFmsData<LgNgResult>(url, lgClearDataRequest);
        JudgeResult(result.Success, "CommitClearResult", lgClearDataRequest, result);
        return result;
    }
    /// <summary>
    /// 提交清洗机电池进站
    /// </summary>
    /// <param name="lgCellEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitClearEnter(LgCellEnterRequest lgCellEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-clear-enter";
        var result = await client.PostFmsData<LgNgResult>(url, lgCellEnterRequest);
        JudgeResult(result.Success, "CommitClearEnter", lgCellEnterRequest, result);
        return result;
    }
    #endregion

    #region 充放电设备
    /// <summary>
    /// 提交充放电结果
    /// </summary>
    /// <param name="lgFmtResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitFmtResult(LgFmtResultRequest lgFmtResultRequest)
    {
        string url = $"{_apiHead}/fms/api/commit-fmt-result";
        var client = CreateHttpClient();
        var result = await client.PostFmsData<LgNgResult>(url, lgFmtResultRequest);
        JudgeResult(result.Success, "CommitFmtResult", lgFmtResultRequest, result);
        return result;
    }

    /// <summary>
    /// 进行移库前验证
    /// </summary>
    /// <param name="lgRequestBase"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitFmtChangeBox(LgRequestBase lgRequestBase)
    {
        string url = $"{_apiHead}/fms/api/commit-fmt-change-box";
        var client = CreateHttpClient();
        var result = await client.PostFmsData<LgNgResult>(url, lgRequestBase);
        JudgeResult(result.Success, "CommitFmtChangeBox", lgRequestBase, result);
        return result;
    }

    /// <summary>
    /// 充放电库位作业开始、作业结束报告
    /// </summary>
    /// <param name="lgFmtReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitFmtReport(LgFmtReportRequest lgFmtReportRequest)
    {
        string url = $"{_apiHead}/fms/api/charge/commit-fmt-report";
        var client = CreateHttpClient();
        var result = await client.PostFmsData<LgNgResult>(url, lgFmtReportRequest);
        JudgeResult(result.Success, "CommitFmtReport", lgFmtReportRequest, result);
        return result;
    }
    /// <summary>
    /// 设备状态和温度报告
    /// </summary>
    /// <param name="lgFmtEquipStatusRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitFmtEquipStatus(LgFmtEquipStatusRequest lgFmtEquipStatusRequest)
    {
        string url = $"{_apiHead}/fms/api/commit-fmt-equip-status";
        var client = CreateHttpClient();
        var result = await client.PostFmsData<LgNgResult>(url, lgFmtEquipStatusRequest);
        JudgeResult(result.Success, "CommitFmtEquipStatus", lgFmtEquipStatusRequest, result);
        return result;
    }
    /// <summary>
    /// 充放电库位托盘投入状态  R:Requst(当仓库清空时发送） A:Arrive(当托盘进入时请求）
    /// </summary>
    /// <param name="lgTrayReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgFmtLoadResult>> CommitFmtEnter(LgTrayReportRequest lgTrayReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-fmt-enter";
        var result = await client.PostFmsData<LgFmtLoadResult>(url, lgTrayReportRequest);
        JudgeResult(result.Success, "CommitFmtEnter", lgTrayReportRequest, result);
        return result;
    }

    /// <summary>
    /// 充放电库位托盘清空状态  R:Requst(当仓库清空时发送） E:End(已清空）
    /// </summary>
    /// <param name="lgTrayReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitFmtOut(LgTrayReportRequest lgTrayReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-fmt-out";
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayReportRequest);
        JudgeResult(result.Success, "CommitFmtOut", lgTrayReportRequest, result);
        return result;
    }

    /// <summary>
    /// 获取充放电配方
    /// </summary>
    /// <param name="lgTray"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgRecipeResult>> GetFmtRecipe(LgTrayReportRequest lgTray)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/get-fmt-recipe";
        var result = await client.PostFmsData<LgRecipeResult>(url, lgTray);
        JudgeResult(result.Success, "GetFmtRecipe", lgTray, result);
        return result;
    }

    /// <summary>
    /// 获取作业的channel
    /// </summary>
    /// <param name="lgTrayReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgFmtChannelResult>> GetFmtChannel(LgTrayReportRequest lgTrayReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/get-fmt-channel";
        var result = await client.PostFmsData<LgFmtChannelResult>(url, lgTrayReportRequest);
        JudgeResult(result.Success, "GetFmtChannel", lgTrayReportRequest, result);
        return result;
    }
    #endregion

    #region 预充
    /// <summary>
    /// 充放电库位托盘投入状态  R:Requst(当仓库清空时发送） A:Arrive(当托盘进入时请求）
    /// </summary>
    /// <param name="lgTrayReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgPreChargeLoadResult>> CommitPreChargeEnterOut(LgTrayReportRequest lgTrayReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-pre-charge-enter-out";
        var result = await client.PostFmsData<LgPreChargeLoadResult>(url, lgTrayReportRequest);
        JudgeResult(result.Success, "CommitPreChargeEnterOut", lgTrayReportRequest, result);
        return result;
    }
    #endregion

    #region 后氦检机
    /// <summary>
    /// 后氦检进站
    /// </summary>
    /// <param name="lgCellEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitHeiEnter(LgCellEnterRequest lgCellEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-hei-enter";
        var result = await client.PostFmsData<LgNgResult>(url, lgCellEnterRequest);
        JudgeResult(result.Success, "CommitHeiEnter", lgCellEnterRequest, result);
        return result;
    }
    /// <summary>
    /// 提交后氦检机数据结果
    /// </summary>
    /// <param name="lgHeiResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitHeiResult(LgHeiResultRequest lgHeiResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-hei-result";
        var result = await client.PostFmsData<LgNgResult>(url, lgHeiResultRequest);
        JudgeResult(result.Success, "CommitHeiResult", lgHeiResultRequest, result);
        return result;
    }
    #endregion

    #region 外观检测机器
    /// <summary>
    /// 提交外观检测结果
    /// </summary>
    /// <param name="lgInspectResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<List<LgNgCellResult>>> CommitInspectResult(LgInspectResultRequest lgInspectResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-inspect-result";
        var result = await client.PostFmsData<List<LgNgCellResult>>(url, lgInspectResultRequest);
        JudgeResult(result.Success, "CommitInspectResult", lgInspectResultRequest, result);
        return result;
    }
    #endregion

    #region 包膜机
    /// <summary>
    /// 提交包膜机结果
    /// </summary>
    /// <param name="lgLaminateResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitLaminateResult(LgLaminateResultRequest lgLaminateResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-laminate-result";
        var result = await client.PostFmsData<LgNgResult>(url, lgLaminateResultRequest);
        JudgeResult(result.Success, "CommitLaminateResult", lgLaminateResultRequest, result);
        return result;
    }

    /// <summary>
    /// 包膜机电芯投入请求判定条件
    /// </summary>
    /// <param name="lgCellEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgLaminateCellResult>> CommitLaminateEnter(LgCellEnterRequest lgCellEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-laminate-enter";
        var result = await client.PostFmsData<LgLaminateCellResult>(url, lgCellEnterRequest);
        JudgeResult(result.Success, "CommitLaminateEnter", lgCellEnterRequest, result);
        return result;
    }
    #endregion


    #region 二次注液机
    /// <summary>
    /// 提交二次注液结果数据
    /// </summary>
    /// <param name="lgLiquidResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitLiquidResult(LgLiquidResultRequest lgLiquidResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-liquid-result";
        var result = await client.PostFmsData<LgNgResult>(url, lgLiquidResultRequest);
        JudgeResult(result.Success, "CommitLiquidResult", lgLiquidResultRequest, result);
        return result;
    }

    /// <summary>
    /// 二次注液机电芯投入请求判定条件
    /// </summary>
    /// <param name="lgCellEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgLiquidCellResult>> CommitLiquidEnter(LgCellEnterRequest lgCellEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/injection/commit-liquid-enter";
        var result = await client.PostFmsData<LgLiquidCellResult>(url, lgCellEnterRequest);
        JudgeResult(result.Success, "CommitLiquidEnter", lgCellEnterRequest, result);
        return result;
    }
    #endregion


    #region 充氦打钉机
    /// <summary>
    /// 充氦打钉机提交结果
    /// </summary>
    /// <param name="lgNailingResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitNailingResult(LgNailingResultRequest lgNailingResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-nailing-result";
        var result = await client.PostFmsData<LgNgResult>(url, lgNailingResultRequest);
        JudgeResult(result.Success, "CommitNailingResult", lgNailingResultRequest, result);
        return result;
    }

    /// <summary>
    /// 充氦打钉机电芯投入请求判定条件
    /// </summary>
    /// <param name="lgCellEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNailingCellResult>> CommitNailingEnter(LgCellEnterRequest lgCellEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-nailing-enter";
        var result = await client.PostFmsData<LgNailingCellResult>(url, lgCellEnterRequest);
        JudgeResult(result.Success, "CommitNailingEnter", lgCellEnterRequest, result);
        return result;
    }
    #endregion

    #region OCV
    /// <summary>
    /// 提交OCV结果
    /// </summary>
    /// <param name="lgOcvResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitOcvResult(LgOcvResultRequest lgOcvResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-ocv-result";
        var result = await client.PostFmsData<LgNgResult>(url, lgOcvResultRequest);
        JudgeResult(result.Success, "CommitOcvResult", lgOcvResultRequest, result);
        return result;
    }

    /// <summary>
    /// Ocv作业开始、作业结束报告
    /// </summary>
    /// <param name="lgFmtReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgOcvReportResult>> CommitOcvReport(LgOcvReportRequest lgFmtReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-ocv-report";
        var result = await client.PostFmsData<LgOcvReportResult>(url, lgFmtReportRequest);
        JudgeResult(result.Success, "CommitOcvReport", lgFmtReportRequest, result);
        return result;
    }

    /// <summary>
    /// 当托盘进入后，触发接口进行验证
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgOcvTrayEnterResult>> CommitOcvTrayEnter(LgTrayEnterRequest lgTrayEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-ocv-tray-enter";
        var result = await client.PostFmsData<LgOcvTrayEnterResult>(url, lgTrayEnterRequest);
        JudgeResult(result.Success, "CommitOcvTrayEnter", lgTrayEnterRequest, result);
        return result;
    }


    /// <summary>
    /// 获取OCV作业配方
    /// </summary>
    /// <param name="lgOcvReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgOcvRecipe>> GetOcvRecipe(LgOcvReportRequest lgOcvReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/get-ocv-recipe";
        var result = await client.PostFmsData<LgOcvRecipe>(url, lgOcvReportRequest);
        JudgeResult(result.Success, "GetOcvRecipe", lgOcvReportRequest, result);
        return result;
    }
    #endregion

    #region 密封钉焊机
    /// <summary>
    /// 密封钉焊机提交结果
    /// </summary>
    /// <param name="lgSealNailResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<List<LgCellNg>>> CommitSealNailResult(LgSealNailResultRequest lgSealNailResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-seal-nail-result";
        var result = await client.PostFmsData<List<LgCellNg>>(url, lgSealNailResultRequest);
        JudgeResult(result.Success, "CommitSealNailResult", lgSealNailResultRequest, result);
        return result;

    }

    /// <summary>
    /// 电芯投入
    /// </summary>
    /// <param name="lgSealNailEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgSealNailCellResult>> CommitSealNailEnter(LgSealNailEnterCellRequest lgSealNailEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-seal-nail-enter";
        var result = await client.PostFmsData<LgSealNailCellResult>(url, lgSealNailEnterRequest);
        JudgeResult(result.Success, "CommitSealNailEnter", lgSealNailEnterRequest, result);
        return result;
    }
    #endregion


    #region 筛选机
    /// <summary>
    /// 每次挑选提交结果，可以不作处理，非必须
    /// </summary>
    /// <param name="lgSelResultRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitSelResult(LgSelResultRequest lgSelResultRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-sel-result";
        var result = await client.PostFmsData<LgNgResult>(url, lgSelResultRequest);
        JudgeResult(result.Success, "CommitSelResult", lgSelResultRequest, result);
        return result;
    }
    /// <summary>
    /// 作业开始、结束
    /// </summary>
    /// <param name="lgSelReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitSelReport(LgSelReportRequest lgSelReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-sel-report";
        var result = await client.PostFmsData<LgNgResult>(url, lgSelReportRequest);
        JudgeResult(result.Success, "CommitSelReport", lgSelReportRequest, result);
        return result;
    }
    /// <summary>
    /// 托盘投入
    /// </summary>
    /// <param name="lgSelReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitSelEnter(LgSelReportRequest lgSelReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-sel-enter";
        var result = await client.PostFmsData<LgNgResult>(url, lgSelReportRequest);
        JudgeResult(result.Success, "CommitSelEnter", lgSelReportRequest, result);
        return result;
    }
    /// <summary>
    /// 托盘投出
    /// </summary>
    /// <param name="lgSelReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitSelOut(LgSelReportRequest lgSelReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-sel-out";
        var result = await client.PostFmsData<LgNgResult>(url, lgSelReportRequest);
        JudgeResult(result.Success, "CommitSelOut", lgSelReportRequest, result);
        return result;
    }
    /// <summary>
    /// 获取筛选配方
    /// </summary>
    /// <param name="lgTrayReportRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgSelRecipe>> GetSelRecipe(LgTrayReportRequest lgTrayReportRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/get-sel-recipe";
        var result = await client.PostFmsData<LgSelRecipe>(url, lgTrayReportRequest);
        JudgeResult(result.Success, "GetSelRecipe", lgTrayReportRequest, result);
        return result;
    }
    #endregion

    #region 公用
    /// <summary>
    /// 当托盘进入后，触发接口进行验证
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitTrayEnter(LgTrayEnterRequest lgTrayEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-tray-enter";
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayEnterRequest);
        JudgeResult(result.Success, "CommitTrayEnter", lgTrayEnterRequest, result);
        return result;
    }
    /// <summary>
    /// 当托盘出去后，触发接口进行验证
    /// </summary>
    /// <param name="lgTrayOutRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitTrayOut(LgTrayOutRequest lgTrayOutRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-tray-out";
        var result = await client.PostFmsData<LgNgResult>(url, lgTrayOutRequest);
        JudgeResult(result.Success, "CommitTrayOut", lgTrayOutRequest, result);
        return result;
    }


    /// <summary>
    /// 当电芯进入后，触发接口进行验证
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitCellEnter(LgCellEnterRequest lgCellEnterRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-cell-enter";
        var result = await client.PostFmsData<LgNgResult>(url, lgCellEnterRequest);
        JudgeResult(result.Success, "CommitCellEnter", lgCellEnterRequest, result);
        return result;
    }
    /// <summary>
    /// 当电芯出站后，触发接口进行验证
    /// </summary>
    /// <param name="lgTrayEnterRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitCellOut(LgCellOutRequest lgCellOutRequest)
    {
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-cell-out";
        var result = await client.PostFmsData<LgNgResult>(url, lgCellOutRequest);
        JudgeResult(result.Success, "CommitCellOut", lgCellOutRequest, result);
        return result;
    }

    /// <summary>
    /// 火灾报警
    /// </summary>
    /// <param name="lgFireRequest"></param>
    /// <returns></returns>
    public async Task<AdminCodeResult<LgNgResult>> CommitFire(LgFireRequest lgFireRequest)
    {
        var flag = lgFireRequest.Type == 0 ? "复位" : "报警";

        await _signalRService.WriteMessage(new XT.Common.Models.SignalR.InformModel
        {
            Sender = lgFireRequest.EquipNum,
            Message = $"库位号{lgFireRequest.LocationNum} 位置 {lgFireRequest.LocationNum} 火警"
        });
        var client = CreateHttpClient();
        string url = $"{_apiHead}/fms/api/commit-fire";
        var result = await client.PostFmsData<LgNgResult>(url, lgFireRequest);
        JudgeResult(result.Success, "CommitFire", lgFireRequest, result);
        return new AdminCodeResult<LgNgResult>();
    }

    /// <summary>
    /// 获取所有电芯通道
    /// </summary>
    /// <param name="lgCellEnterRequest"></param>
    /// <returns></returns>
    public async Task<List<FMTCell>> TrayCellChannel(LgTrayEnterRequest lgTrayEnterRequest)
    {
        return new List<FMTCell>();
    }
    #endregion


    #region 调度


    #endregion

    /// <summary>
    /// 触发弹窗
    /// </summary>
    /// <param name="type"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public async Task AvaloniaAlert(int type, string message)
    {
        if (type == 1)
        {
            _signalRService.WriteMessage(new XT.Common.Models.SignalR.InformModel
            {
                Message = message,
                Sender = "Mock",
            });
        }
        else
        {
            _signalRService.WriteLog(new XT.Common.Models.SignalR.RemoteLog
            {
                Content = message,
                IsToRemote = true,
            });
        }


    }
    /// <summary>
    /// Fms返回结果判定报错处理
    /// </summary>
    /// <param name="result">是否成功</param>
    /// <param name="ApiName">j接口名称</param>
    /// <param name="inputParam">请求参数</param>
    /// <param name="result">返回结果</param>
    public void JudgeResult(bool success, string ApiName, object inputParam, object result)
    {
        string logContent = $"Api:{ApiName},inputParam:{inputParam.ToJson()},result:{result.ToJson()}";
        logContent = Regex.Unescape(logContent);
        var para = inputParam.Adapt<LgRequestBase>();

        if (success)
        {
            _logService.Log($"EquipType：{para.EquipType},EquipNum：{para.EquipNum},{logContent}");
        }
        else
        {
            _logService.LogError($"EquipType：{para.EquipType},EquipNum：{para.EquipNum},{logContent}");
            _logService.LogContent(logContent, LogEnum.Error, para.EquipNum, para.EquipType.ToString());
            //AvaloniaAlert(1, $"设备{equipNum}{funcName}：{message}");
        }
    }


    public void LogData(string title, object data)
    {
        var str = data.ToJson();
        _logService.Log($"title:{title},data:{str}");
    }

    public void LogMessage(string content)
    {
        _logService.LogHeart(content);
    }
}
